package com.mvvmhabit.org.binding.viewadapter.textview

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Matrix
import android.graphics.drawable.BitmapDrawable
import android.os.Handler
import android.os.Message
import android.text.Html
import android.text.method.LinkMovementMethod
import android.widget.TextView
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.mvvmhabit.org.R
import com.mvvmhabit.org.utils.RichTextUtils


@BindingAdapter(value = ["htmlString"])
fun setHtmlText(textView: TextView, htmlString: String?) {
    if (!htmlString.isNullOrEmpty()) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            /*textView.text = Html.fromHtml(
                htmlString,
                Html.FROM_HTML_MODE_COMPACT,
                getImageGetter(textView.context, textView),
                null
            )*/
            //  getImageGetter(textView.context, textView)
            RichTextUtils.showRichHtmlWithImageContent(textView, htmlString)
        } else {
            //getImageGetter(textView.context, textView)
            RichTextUtils.showRichHtmlWithImageContent(textView, htmlString)
            /* textView.text =
                 Html.fromHtml(htmlString, getImageGetter(textView.context, textView), null)*/
        }
    }
}

fun getImageGetter(context: Context, textView: TextView): Html.ImageGetter {
    var drawable: URLDrawable? = null
    return Html.ImageGetter {
        // Target
        Glide.with(context).asBitmap()
            .apply(RequestOptions().circleCrop().placeholder(R.mipmap.ic_launcher))
            .load(it).into(object : SimpleTarget<Bitmap>() {
                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                    drawable = URLDrawable(changeBitmapSize(resource), context.resources)
                    drawable?.setBounds(
                        0,
                        0,
                        changeBitmapSize(resource)!!.width,
                        changeBitmapSize(resource)!!.height
                    )
                    textView.invalidate()
                }
            })
        textView.text = textView.text
        return@ImageGetter drawable
    }
}

class URLDrawable(private val bitmaps: Bitmap?, resource: Resources?) :
    BitmapDrawable(resource, bitmaps) {

    override
    fun draw(canvas: Canvas) {
        if (bitmaps != null) {
            canvas.drawBitmap(bitmaps, 0F, 0F, paint)
        }
    }
}

private fun changeBitmapSize(bitmap: Bitmap): Bitmap? {
    var bitmap = bitmap
    val width = bitmap.width
    val height = bitmap.height
    //设置想要的大小
    //计算压缩的比率
    val scaleWidth = width.toFloat() / width
    val scaleHeight = height.toFloat() / height
    //获取想要缩放的matrix
    val matrix = Matrix()
    matrix.postScale(scaleWidth, scaleHeight)
    //获取新的bitmap
    bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true)
    bitmap.width
    bitmap.height
    return bitmap
}

